<template>
  <el-container class="home">
    <el-header class="header">
      <img src="../assets/image/heima.png" />
      <h3>电商后台管理系统</h3>
      <el-button @click="tuichu">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="collapse ? '64px' : '200px'" class="aside">
        <div class="toggle-aside" @click="collapse = !collapse">|||</div>
        <!-- 导航菜单 -->
        <el-menu
          class="el-menu-vertical-demo"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          :unique-opened="true"
          :collapse="collapse"
          :collapse-transition="false"
          :router="true"
        >
          <el-submenu
            :index="item.path"
            v-for="(item, index) in menuList"
            :key="item.id"
          >
            <template slot="title">
              <i :class="iconList[index]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <el-menu-item
              :index="subItem.path"
              v-for="(subItem, subIndex) in item.children"
              :key="subItem.id"
              :cc="subIndex"
            >
              <i class="el-icon-menu"></i>
              <span>{{ subItem.authName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main class="main">
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menuList: [],
      iconList: [
        "el-icon-user-solid",
        "el-icon-s-promotion",
        "el-icon-s-goods",
        "el-icon-s-order",
        "el-icon-s-marketing"
      ],
      collapse: false
    };
  },
  methods: {
    tuichu: function() {
      window.sessionStorage.removeItem("token");
      this.$router.push("/login");
    },
    // 渲染左侧菜单栏
    handleLeftMenu: async function() {
      // 发起ajax请求
      const { data: res } = await this.$http.get("menus");
      // console.log(res);
      if (res.meta.status !== 200) {
        return this.$message.error("获取菜单数据失败");
      }
      this.menuList = res.data;
      // console.log(this.menuList);
    }
  },
  created: function() {
    this.handleLeftMenu();
  }
};
</script>

<style lang="less" scoped>
.home {
  height: 100%;
}
.header {
  background-color: #373d41;
  user-select: none;
  img {
    float: left;
    margin-top: 2px;
    margin-right: 10px;
  }
  h3 {
    margin: 0px;
    float: left;
    color: #fff;
    font-weight: normal;
    font-size: 22px;
    line-height: 60px;
  }
  .el-button {
    float: right;
    margin-top: 10px;
  }
}
.aside {
  background-color: #313541;
  .toggle-aside {
    background-color: #4a5064;
    text-align: center;
    height: 22px;
    font-size: 12px;
    color: #fff;
    line-height: 20px;
    letter-spacing: 1px;
    user-select: none;
  }
  .el-menu {
    border-right: none;
  }
}

.main {
  background-color: #eaedf1;
}
</style>
